package com.stonedt.spider.controller.bidding;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.stonedt.spider.util.DateUtil;
import com.stonedt.spider.util.TimesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
import com.stonedt.spider.service.BiddingService;

@Controller
@RequestMapping("/bidding")
public class BiddingController {
    /**
     * 跳转招标页面并且渲染数据
     */
    @Autowired
    BiddingService biddingService;

    @RequestMapping(value = "/getBiddingList", method = RequestMethod.GET)
    public ModelAndView getBiddingList(ModelAndView mv, String informationtype,
                                       @RequestParam(value = "pageNum", defaultValue = "1", required = false) String pageNum) {
        mv.setViewName("bidding/biddingList");
        Map<String, Object> map = new HashMap<>();
        map.put("informationtype", informationtype);
        map.put("keyword", "");
        map.put("timee", "");
        map.put("times", "");
        map.put("page", pageNum);
        map.put("size", "20");

        JSONObject returnObj = biddingService.getBiddingList(map);
        mv.addObject("biddingList", returnObj);

        return mv;
    }

    /**
     * 政府
     */
    @RequestMapping(value = "/bidding-gov", method = RequestMethod.GET)
    public ModelAndView toBiddingPage(ModelAndView mv, String informationtype, HttpServletRequest request,
    		@RequestParam(value = "website_id", defaultValue = "1", required = false) String website_id) {
    	mv.addObject("nowArray", DateUtil.getNowDate());
    	mv.setViewName("bidding/bidding-gov");
        request.setAttribute("LEFT", "bidding-gov");
        return mv;
    }
    /**
     * 电网
     */
    @RequestMapping(value = "/bidding-power", method = RequestMethod.GET)
    public ModelAndView power(ModelAndView mv, String informationtype, HttpServletRequest request,
    		@RequestParam(value = "website_id", defaultValue = "1", required = false) String website_id){
    	mv.addObject("nowArray", DateUtil.getNowDate());
    	mv.setViewName("bidding/bidding-power");
        request.setAttribute("LEFT", "bidding-power");
        return mv;
    }
    /**
     * 电信
     */
    @RequestMapping(value = "/bidding-telecom", method = RequestMethod.GET)
    public ModelAndView telecom(ModelAndView mv, String informationtype, HttpServletRequest request,
    		@RequestParam(value = "website_id", defaultValue = "1", required = false) String website_id) {
    	mv.addObject("nowArray", DateUtil.getNowDate());
    	mv.setViewName("bidding/bidding-telecom");
        request.setAttribute("LEFT", "bidding-telecom");
        return mv;
    }
    /**
     * 银行
     */
    @RequestMapping(value = "/bidding-bank", method = RequestMethod.GET)
    public ModelAndView bank(ModelAndView mv, String informationtype, HttpServletRequest request,
                                      @RequestParam(value = "website_id", defaultValue = "1", required = false) String website_id) {
    	mv.addObject("nowArray", DateUtil.getNowDate());
    	mv.setViewName("bidding/bidding-bank");
        request.setAttribute("LEFT", "bidding-bank");
        return mv;
    }
    /**
     * 特种
     */
    @RequestMapping(value = "/bidding-special", method = RequestMethod.GET)
    public ModelAndView special(ModelAndView mv, HttpServletRequest request,
                                      @RequestParam(value = "website_id", defaultValue = "1", required = false) String website_id) {
    	mv.addObject("nowArray", DateUtil.getNowDate());
    	mv.setViewName("bidding/bidding-special");
        request.setAttribute("LEFT", "bidding-special");
        return mv;
    }
    /**
     * 电铁栏目
     */
    @RequestMapping(value = "/bidding-electric_railway", method = RequestMethod.GET)
    public ModelAndView electric_railway(ModelAndView mv, HttpServletRequest request,
                                      @RequestParam(value = "website_id", defaultValue = "1", required = false) String website_id) {
    	mv.addObject("nowArray", DateUtil.getNowDate());
    	mv.setViewName("bidding/bidding-electric_railway");
        request.setAttribute("LEFT", "bidding-electric_railway");
        return mv;
    }
    /**
     * 综合
     */
    @RequestMapping(value = "/bidding-synthesize", method = RequestMethod.GET)
    public ModelAndView synthesize(ModelAndView mv, HttpServletRequest request,
                                      @RequestParam(value = "website_id", defaultValue = "1", required = false) String website_id) {
    	mv.addObject("nowArray", DateUtil.getNowDate());
    	mv.setViewName("bidding/bidding-synthesize");
        request.setAttribute("LEFT", "bidding-synthesize");
        return mv;
    }
    /**
     *
     */
    @RequestMapping(value = "/getBiddingListAsyn", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject getBiddingListAsyn(
            @RequestBody JSONObject data) {
	   System.out.println(data.toJSONString());
        Map<String, Object> map = new HashMap<>();
        map = JSONObject.parseObject(data.toJSONString(), Map.class);
        JSONObject timesJson =  TimesUtil.getDifferDayTimes();
        String startTime = timesJson.getString("TIMEBEGIN");
        String endTime = timesJson.getString("TIMEEND");
//        map.put("informationtype", informationtype);
//        map.put("keyword", keywords);
        String website_id = map.get("website_id").toString();
        map.put("website_id", website_id);
        map.put("timee", endTime);
        map.put("times", startTime);
//        map.put("province", province);
//        map.put("province", province);
//        map.put("city", city);
        map.put("size", "20");
        JSONObject returnObj = biddingService.getBiddingList(map);
        return returnObj;
    }
    
    @RequestMapping(value = "/getSpecialList", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject getSpecialList(@RequestBody JSONObject data) {
    	System.out.println(data.toJSONString());
        Map<String, Object> map = new HashMap<>();
        map = JSONObject.parseObject(data.toJSONString(), Map.class);
        JSONObject timesJson =  TimesUtil.getDifferDayTimes();
        String startTime = timesJson.getString("TIMEBEGIN");
        String endTime = timesJson.getString("TIMEEND");
        String website_id = map.get("website_id").toString();
        map.put("website_id", website_id);
        map.put("timee", endTime);
        map.put("times", startTime);
        map.put("size", "20");
        JSONObject returnObj = biddingService.getSpecialList(map);
        return returnObj;
    }
    
    @RequestMapping(value = "/getElectricRailwayList", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject getElectricRailwayList(@RequestBody JSONObject data) {
    	System.out.println(data.toJSONString());
        Map<String, Object> map = new HashMap<>();
        map = JSONObject.parseObject(data.toJSONString(), Map.class);
        JSONObject timesJson =  TimesUtil.getDifferDayTimes();
        String startTime = timesJson.getString("TIMEBEGIN");
        String endTime = timesJson.getString("TIMEEND");
        String website_id = map.get("website_id").toString();
        map.put("website_id", website_id);
        map.put("timee", endTime);
        map.put("times", startTime);
        map.put("size", "20");
        JSONObject returnObj = biddingService.getElectricRailwayList(map);
        return returnObj;
    }
    
    @RequestMapping(value = "/getSynthesizeList", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject getSynthesizeList(@RequestBody JSONObject data) {
    	System.out.println(data.toJSONString());
        Map<String, Object> map = new HashMap<>();
        map = JSONObject.parseObject(data.toJSONString(), Map.class);
        JSONObject timesJson =  TimesUtil.getDifferDayTimes();
        String startTime = timesJson.getString("TIMEBEGIN");
        String endTime = timesJson.getString("TIMEEND");
        String website_id = map.get("website_id").toString();
        map.put("website_id", website_id);
        map.put("timee", endTime);
        map.put("times", startTime);
        map.put("size", "20");
        JSONObject returnObj = biddingService.getSynthesizeList(map);
        return returnObj;
    }
    
    /*@RequestMapping(value = "/getBiddingListAsyn", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject getBiddingListAsyn(
            @RequestBody JSONObject data,String website_id) {
        Map<String, Object> map = new HashMap<>();
        map = JSONObject.parseObject(data.toJSONString(), Map.class);
        JSONObject timesJson =  TimesUtil.getDifferDayTimes();
        String startTime = timesJson.getString("TIMEBEGIN");
        String endTime = timesJson.getString("TIMEEND");
        
        JSONObject end_json=new JSONObject();
        JSONObject json=new JSONObject();
        JSONArray  array=new JSONArray();
        json.put("field", "website_id");
        json.put("keyword", website_id);
        array.add(json);
        end_json.put("timefield", "publish_time");
        end_json.put("times", startTime);
        end_json.put("timee", endTime);
        end_json.put("index", "stonedt_biao");
        end_json.put("type", "infor");
        end_json.put("page", map.get("page"));
        end_json.put("size", "20");
        end_json.put("and", array);
        JSONObject returnObj = biddingService.getBiddingList(map);
        return returnObj;
    }*/


    /**
     * 获取招标详细信息
     */
    @RequestMapping(value = "/getInformationtypeDeatil", method = RequestMethod.GET)
    public ModelAndView getInformationtypeDeatil(ModelAndView mv, String article_public_id) {
        try {
            mv.setViewName("bidding/biddingDetail");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("article_public_id", article_public_id);
            JSONObject returnObj = biddingService.getBiddingDetail(map);
            if(returnObj.get("detailurl")!=""&&returnObj.get("detailurl")!=null) {
            	returnObj.put("detail_url", returnObj.get("detailurl"));
            }
            mv.addObject("biddingData", returnObj);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
    
    /**
     * 异步加载省份
     */
    @RequestMapping(value = "/getProvinceAsyn", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getProvinceAsyn() {
        JSONObject responseData = new JSONObject();
        try {
            Map<String, String> map = new HashMap<>();
            responseData = biddingService.getProvinceAsyn(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }


    /**
     * 获取城市信息
     */
    @RequestMapping(value = "/getCityAsyn", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getCityAsyn(@RequestParam(value = "proviceName", defaultValue = "", required = true) String proviceName) {
        JSONObject responseData = new JSONObject();
        try {
            Map<String, String> map = new HashMap<>();
            map.put("province", proviceName);
            responseData = biddingService.getCityAsyn(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }

    /**
     * 获取行业信息
     */
    @RequestMapping(value = "/getIndustryAsyn", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getIndustryAsyn() {
        JSONObject responseData = new JSONObject();
        try {
            Map<String, String> map = new HashMap<>();
            responseData = biddingService.getIndustryAsyn(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }


    /**
     * 获取业务类型数据
     */
    @RequestMapping(value = "/getbusinessTypeAsyn", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getbusinessTypeAsyn() {
        JSONObject responseData = new JSONObject();
        try {
            Map<String, String> map = new HashMap<>();
            responseData = biddingService.getbusinessTypeAsyn(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseData;
    }


    /***
     *异步加载信息类型
     * @param informationtype
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/getInformationtypeAsyn", method = RequestMethod.GET)
    @ResponseBody
    public JSONArray getInformationtypeAsyn() {
        JSONArray returnArray = biddingService.getInformationtype();
        return returnArray;
    }

}
